package minio

import (
	"comic/internal/pb"
	"common"
	"common/zapx"
	"errors"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"time"
)

// GetComicFullThumb 获取完整的漫画封面图
func GetComicFullThumb(thumb string) (string, error) {
	conn, err := grpc.Dial(
		common.ConsulSerUrl("minio"),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
	)
	if err != nil {
		zapx.Logger.Error("连接minio服务失败", zap.Error(err))
		return "", errors.New("获取封面失败")
	}
	defer conn.Close()
	client := pb.NewMinioServiceClient(conn)
	ctx, cancel := common.WithTimeoutContext(time.Second * 2)
	defer cancel()
	res, err := client.GetUrl(ctx, &pb.UrlRequest{Key: thumb})
	if err != nil {
		zapx.Logger.Error("通过minio获取连接失败", zap.Error(err))
		return "", errors.New("获取封面失败")
	}
	return res.Uri, nil
}
